<?php
namespace Home\Controller;

use Common\Controller\PcApiController;

class PcRefundController extends PcApiController {

    public $timestamp;

    public function _initialize() {
        parent::_initialize();
        $this->timestamp = time();
        if(!IS_POST){
            $this->ajaxReturn(1, '提交方式错误！');
        }
    }

    /**
     * index
     * 企业退款详情
     * 通过顾问找人才 通过悬赏找人才 退款保证金
     * @access public
     * @param int       $id (必填：是) 订单ID
     * @since
     * @return json
     */
    public function index(){
        $id     = I('post.id', 0, 'intval');
        $user   = $this->userinfo;
        if($this->userinfo['utype']<>1) $this->ajaxReturn(1, '当前账号类型不能操作退款');

        if(empty($id)) $this->ajaxReturn(1, '传递的参数有误');

        $order  = M('Order')->where(['id'=>$id,'uid'=>$user['uid']])->find();
        if(empty($order)) $this->ajaxReturn(1, '退款记录不存在');

//        if(!in_array($order['fa_type'], [2,3]) || $order['refund_status']<>0) $this->ajaxReturn(1,'当前记录已申请过退款');

        $company    = M('CompanyProfile')->where(['uid'=>$user['uid'], 'audit'=>1])->find();
        if(empty($company)) $this->ajaxReturn(1, '企业信息不存在');

        if(empty($order['jobs_id']) || empty($company['id']))  $this->ajaxReturn(1, '获取参数不完整');

        //查询企业简历下载记录
        $companyDown    = M('CompanyDownResume')->where(['company_uid'=>$user['uid']])->getField('resume_uid', true);

        $gwTuiJian  = M('GuwenTuiJianRusume')->where(['resume_uid'=>['in', $companyDown],'jobs_id'=> $order['jobs_id'], 'comp_uid'=>$user['uid'], ' tui_status'=>['in', [0,1]]])->find();
        if(!empty($gwTuiJian)) $this->ajaxReturn(1, '当前订单记录没有处理完，请处理完后再申请退款');

        $GuWenJieDanOrder  = M("GuWenJieDanOrder")->where(['fa_bu_order'=>$order['id']])->getField('id', true);
        $payMoney   = 0;
        $list       = [];
        if($GuWenJieDanOrder){
            $moneyList  = M("GuwenMoneyLog")->where(['order_type'=>'service','order_id'=>['in', $GuWenJieDanOrder]])->select();
            foreach($moneyList as $val){
                $payMoney+=$val['all_money'];
                $desc='';
                if($val['ruzhi_type']==1){
                    $desc='入职扣费';
                }else{
                   $desc="入职满月扣费";
                }
                $user_names=M('resume')->where(['uid'=>$val['lu_uid']])->getField('fullname');
                $newval = [
                    'money'     => $val['all_money'],
                    'add_time'  => date('Y-m-d', $val['add_time']),
                    'fullname'  => $user_names,
                    'desc'      => $desc
                ];
//                $tui_users  = M('guwen_tui_jian_rusume')->where(['guwen_uid'=>$val['gw_uid'],'order_id'=>$val['order_id'],'tui_status'=>['in',[1,3,4]]])->getField('resume_uid',true);
//                if($tui_users) {
//                    $user_names = M('resume')->where(['uid' => ['in', $tui_users]])->getField('fullname', true);
//                    $name_str = implode(',', $user_names);
//                    $newval['fullname']  = $name_str;
//                }

                $list[] = $newval;
            }
        }

        $data   = [
            'oid'           => $order['oid'],
            'service_money' => $order['service_money'],//保证金
            'payMoney'      => $payMoney, //已使用保证金
            'remainingMoney'=> $order['service_money']-$payMoney,//剩余金额
            'payment_time'  => date('Y-m-d', $order['payment_time']),
            'list'          => $list//消费记录
        ];

        $this->ajaxReturn(0,'', $data);
    }

    /**
     * requestRefund
     * 企业退款

     * 通过顾问找人才 通过悬赏找人才 退款保证金

     * @access public

     * @param int       $id (必填：是) 订单ID

     * @since

     * @return json

     */
    public function requestRefund(){
        $id     = I('post.id', 0, 'intval');
        $user   = $this->userinfo;
        if($this->userinfo['utype']<>1) $this->ajaxReturn(1, '当前账号类型不能操作退款');

        if(empty($id)) $this->ajaxReturn(1, '传递的参数有误');

        $order  = M('Order')->where(['id'=>$id,'uid'=>$user['uid']])->find();
        if(empty($order)) $this->ajaxReturn(1, '退款记录不存在');

        if(!in_array($order['fa_type'], [2,3]) || $order['refund_status']<>0) $this->ajaxReturn(1,'当前记录已申请过退款');

        $company    = M('CompanyProfile')->where(['uid'=>$user['uid'], 'audit'=>1])->find();
        if(empty($company)) $this->ajaxReturn(1, '企业信息不存在');

        if(empty($order['jobs_id']) || empty($company['id']))  $this->ajaxReturn(1, '获取参数不完整');


        //查询企业简历下载记录
        $companyDown    = M('CompanyDownResume')->where(['company_uid'=>$user['uid']])->getField('resume_uid', true);

        $gwTuiJian  = M('GuwenTuiJianRusume')->where(['resume_uid'=>['in', $companyDown],'jobs_id'=> $order['jobs_id'], 'comp_uid'=>$user['uid'], ' tui_status'=>['in', [0,1]]])->find();

        if(!empty($gwTuiJian)) $this->ajaxReturn(1, '当前订单记录没有处理完');
        $GuWenJieDanOrder  = M("GuWenJieDanOrder")->where(['fa_bu_order'=>$order['id']])->getField('id', true);

    //    $GuWenJieDanOrder   = M("GuWenJieDanOrder")->where(['fa_bu_order'=>$order['id']])->getField('id', true);
        $money              = 0;
        if($GuWenJieDanOrder) {
            $money = M("GuwenMoneyLog")->where(['order_type'=>'service','order_id' => ['in', $GuWenJieDanOrder]])->sum('all_money');
        }

        if(M('Refund')->where(['order_pay_id' => $order['id'], 'jobs_id' => $order['jobs_id'],'company_id'    => $company['id']])->find()){
            $this->ajaxReturn(1, '当前职位退款申请记录已经存在，请勿重复申请');
        }

        $jobs   = M("Jobs")->where(['id' => $order['jobs_id']])->find();

        $remainingMoney = $order['service_money']-$money;
        $data   = [
            'uid'           => $user['uid'],
            'jobs_id'       => $order['jobs_id'],
            'order_pay_id'  => $order['id'],
            'payment'       => $order['payment'],
            'payment_cn'    => $order['payment_cn'],
            'company_id'    => $company['id'],
            'jobs_name'     => !empty($jobs['jobs_name']) ? $jobs['jobs_name'] : '无',
            'companyname'   => $company['companyname'],
            'money'         => $remainingMoney,
            'status'        => 1,
            'addtime'       => time(),
        ];

        if(M('Refund')->add($data)){
            $roid = "W-" . date('ymd', time()) . "-" . date('His', time()) . "-" . mt_rand(1000, 9999);
            M('Order')->where(['id'=>$id])->save(['refund_status'=>1, 'r_oid'=>$roid]);
            $this->ajaxReturn(0, 'ok');
        }
        $this->ajaxReturn(1, '退款申请添加失败');
    }

    /**
     * processingStatus
     * 退款处理状态

     * 退款处理进度状态

     * @access public

     * @param int       $id (必填：是) 订单ID

     * @since

     * @return json

     */

    public function processingStatus(){
        $id     = I('post.id', 0, 'intval');
        $user   = $this->userinfo;
        if($this->userinfo['utype']<>1) $this->ajaxReturn(1, '当前账号类型不能操作退款');

        if(empty($id)) $this->ajaxReturn(1, '传递的参数有误');

        $order  = M('Order')->where(['id'=>$id,'uid'=>$user['uid']])->find();
        if(empty($order)) $this->ajaxReturn(1, '退款记录不存在');

        $company    = M('CompanyProfile')->where(['uid'=>$user['uid'], 'audit'=>1])->find();
        if(empty($company)) $this->ajaxReturn(1, '企业信息不存在');

        $refund = M('Refund')->where(['order_pay_id' => $order['id'], 'jobs_id' => $order['jobs_id'],'company_id'    => $company['id']])->find();
        if(empty($refund)) $this->ajaxReturn(1, '退款信息不存在');

        $payment_cn = $refund['payment']=='wxpay' ? '微信退款' : ($refund['payment']=='remittance' ? '线下退款' : '工程币退款');

        $data   = [
            'money'     => $refund['money'],
            'payment'   => $refund['payment'],
            'payment_cn'=> $payment_cn,
            'remarks'   => $refund['remarks'],
            'status'    => $refund['status'],
            'uptime'    => !empty($refund['uptime']) ? date('Y-m-d H:i:s', $refund['uptime']) : '',
            'addtime'   => !empty($refund['addtime']) ? date('Y-m-d H:i:s', $refund['addtime']) : ''
        ];

        $this->ajaxReturn(0,'ok', $data);

    }
}

?>